package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.eduservice.service.impl.EduTeacherServiceImpl;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 董万均
 * @since 2020-10-17
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    //从容器中获取service层对象
    @Autowired
    public EduTeacherService service;

    @ApiOperation(value = "所有的讲师")
    @GetMapping("queryAll")
    public Result queryAll(){
        List<EduTeacher> list = service.list(null);
        return Result.ok().data("items",list);//用自己设置的json格式
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public Result delete(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        boolean flag = service.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    //将讲师管理进行分页,current是当前页数，limit是每页中当前条数
    @GetMapping("pageTeacher/{current}/{limit}")
    public Result pageTeacher(@PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> page=new Page<>(current,limit);
        //调用方法时，底层进行了封装，把分页中所有数据封装到page对象中
        service.page(page,null);
        long total = page.getTotal();//获取总的条数
        List<EduTeacher> records = page.getRecords();//获取当前页中条数,每页中的行数
        return Result.ok().data("total",total).data("rows",records);
    }

    //根据各种条件的组合来进行分页查询，例如每页中显示教师名称是什么，教师的等级是什么，教师的开始时间或者结束时间
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                       @RequestBody(required = false) TeacherQuery teacherQuery){//RequestBody是发送json格式数据，将json格式的数据封装成TeacherQuery对象
        //@ResponseBody是返回json格式的数据，required=false表示对象中参数可以为null，RequestBody必须是post请求，而不是get请求
        Page<EduTeacher> teacherPage=new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //动态拼接SQL语句
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        //排序，将最新创建的排在第一个
        wrapper.orderByDesc("gmt_create");
        service.page(teacherPage,wrapper);
        long total=teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody EduTeacher teacher){//required=true表示封装对象中的值不能为null，为null的话变成false
        boolean save = service.save(teacher);
        if (save){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    //修改讲师
    //先查询
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable String id) {
        EduTeacher teacher = service.getById(id);
        return Result.ok().data("teacher",teacher);
    }

    //在修改
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher teacher){
        boolean update = service.updateById(teacher);
        if (update){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}

